package com.ohos.carbon.slice.animations;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class WidgetAnimationsSlice extends AbilitySlice {

    private Image back_button, image1, image2;
    private Text text_title;
    private Button flyBbutton, shoppingButton, stackButton, flyAnimations, shoppingCart;
    private boolean isShowOrNot = false;
    private StackLayout stackLayout;
    private DependentLayout depLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_widget_animations);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Widget animations")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Widget animations")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Widget animations", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Widget animations", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Widget animations", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Widget animations");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        //初始化
        flyBbutton = (Button) findComponentById(ResourceTable.Id_fly_button);
        shoppingButton = (Button) findComponentById(ResourceTable.Id_shopping_button);
        stackButton = (Button) findComponentById(ResourceTable.Id_stack_button);
        flyAnimations = (Button) findComponentById(ResourceTable.Id_fly_animations);
        shoppingCart = (Button) findComponentById(ResourceTable.Id_shopping_cart);
        stackLayout = (StackLayout) findComponentById(ResourceTable.Id_stack_layout);
        depLayout = (DependentLayout) findComponentById(ResourceTable.Id_dep_layout);
        //flyBbutton点击事件
        flyBbutton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = flyAnimations.createAnimatorProperty();
                if (isShowOrNot == false) {
                    animator.moveFromY(300).moveToY(300).alpha(0).setDuration(70);
                    isShowOrNot = true;
                } else {
                    animator.moveFromY(300).moveToY(250).alpha(1).setDuration(70);
                    isShowOrNot = false;
                }
                animator.start();
            }
        });
        shoppingButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //动画
                AnimatorProperty animator = shoppingCart.createAnimatorProperty();
                if (isShowOrNot == false) {
                    animator.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(0).setDuration(70);
                    isShowOrNot = true;
                } else {
                    animator.scaleX(0).scaleXBy(0).scaleY(0).scaleYBy(0).alpha(1).setDuration(70);
                    isShowOrNot = false;
                }
                animator.start();
            }
        });
        DirectionalLayout.LayoutConfig ballScaleIndicatorConfig = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
        ballScaleIndicatorConfig.setMargins(13, 0, 0, 0);
        BallScaleIndicator ballScaleIndicator = new BallScaleIndicator(this);
        ballScaleIndicator.setLayoutConfig(ballScaleIndicatorConfig);
        ballScaleIndicator.setVisibility(Component.INVISIBLE);
        depLayout.addComponent(ballScaleIndicator);
        stackButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ballScaleIndicator.start();
                ballScaleIndicator.setVisibility(Component.VISIBLE);
            }
        });

        //点击返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
        });


    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
